package com.lagou.homeWork4.hw1;
/*
* 使用 List 集合实现简易的学生信息管理系统，要求打印字符界面提示用户选择相应的功 能，根据用户输入的选择去实现增加、删除、修改、查找以及遍历所有学生信息的功能。
* */

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Menu {
    Scanner sc = new Scanner(System.in);
    private static void menu() {
        System.out.println("***********************************************");
        System.out.println("****************学生信息管理系统****************");
        System.out.println("----------------               ----------------");
        System.out.println("1、新增学生信息                 2、查找学生信息");
        System.out.println("3、删除学生信息                 4、修改学生信息");
        System.out.println("5、遍历学生信息                 6、    退出    ");
        System.out.println("***********************************************");
    }

    public static void main(String[] args) throws AgeException, IdException {
        List<Student> listStudents = null;

        // 指向学生集合文件
        File txt = new File("./lagouhomeWork4/hw1/listStudents.txt");
        if (txt.exists()) {
            // 如果文件存在则读取
            listStudents = ReadList.readList();
        }else{
            // 如果不存在则创建一个新集合
            listStudents = new LinkedList<>();
        }

        Scanner sc = new Scanner(System.in);
        int age;
        int id;
        String name;
        while(true) {
            menu();
            System.out.println("请输入您要操作的编号：");
            int s = sc.nextInt();
            switch (s) {
                //添加
                case 1:
                    System.out.println("输入id");
                    id = sc.nextInt();
                    System.out.println("输入姓名");
                    name = sc.next();
                    System.out.println("输入年龄");
                    age = sc.nextInt();
                    StudentAdd.addStudent(id, name, age, listStudents);
                    break;
                //查询
                case 2:
                    System.out.println("输入想查询的id");
                    id = sc.nextInt();
                    Student st = StudentFind.findStudent(id,listStudents);
                    if (null == st) {
                        System.out.println("查询不到该学生！");

                    } else {
                        id = st.getId();
                        name = st.getName();
                        age = st.getAge();
                        System.out.println("搜索成功：");
                        System.out.println("学号：" + id + "姓名:" + name + "年龄：" + age);
                    }
                    break;
                //删除
                case 3:
                    System.out.println("输入需要删除的id");
                    id = sc.nextInt();
                    StudentDel.deleteStudent(id,listStudents);
                    break;
                //修改
                case 4:
                    System.out.println("输入要修改的id");
                    id = sc.nextInt();
                    Student getStudent = StudentFind.findStudent(id, listStudents);
                    if (null == getStudent) {
                        System.out.println("查询不到该学生！");
                        break;
                    }
                    System.out.println("输入id");
                    id = sc.nextInt();
                    System.out.println("输入姓名");
                    name = sc.next();
                    System.out.println("输入年龄");
                    age = sc.nextInt();
                    StudentChange.changeStudent(id, name, age, getStudent);
                    break;
                //遍历
                case 5:
                    StudentShow.showStudents(listStudents);
                    break;
                case 6:
                    System.out.println("正在退出...");
                    WriteList.writeList(listStudents);
                    break;
                default:
                    System.out.println("输入错误,请重新选择选项!");
                    continue;
            }
            if(s == 6){
                System.out.println("欢迎下次使用，再见！");
                break;
            }
        }
        sc.close();
    }

}
